package LinkedList;

public class MyLinkedList {
    static class ListNode {
        int val;
        ListNode next;
        ListNode prev;
        ListNode(int x) {
            this.val = x;
        }
    }
    int size;
    ListNode head;
    ListNode tail;
    //头插法
    public void addFirst(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
        }else {
            ListNode newNode = new ListNode(data);
            newNode.next = head;
            head.prev = newNode;
            head = newNode;
        }
        size++;
    }
    //尾插法
    public void addLast(int data){
        if(head == null){
            head = new ListNode(data);
            tail = head;
        }else {
            ListNode newNode = new ListNode(data);
            tail.next = newNode;
            newNode.prev = tail;
            tail = newNode;
        }
        size++;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size){
            return false;
        }
        if(index == 0){
            addFirst(data);
        }else if(index == size){
            addLast(data);
        }else {
            ListNode newNode = new ListNode(data);
            ListNode cur = head;
            for(int i=0;i<index;i++){
                cur = cur.next;
            }
            newNode.next = cur;
            newNode.prev = cur.prev;
            cur.prev.next = newNode;
            cur.prev = newNode;
            size++;
        }
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        if(cur.val == key){
            if(cur.next == null){
                head = null;
                tail = null;
            }else {
                head = cur.next;
                head.prev = null;
            }
            size--;
            return;
        }
        while(cur != null){
            if(cur.val == key&&cur.next!=null){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                size--;
            }
            if(cur.next == null&&cur.val==key){
                tail = cur.prev;
                tail.next = null;
                size--;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null){
            return;
        }
        ListNode cur = head;
        while(cur.val==key&&cur.next!=null ){
            head = cur.next;
            head.prev = null;
            cur = head;
            size--;
        }
        if(cur.val==key){
            head=null;
            tail=null;
            size=0;
        }
        while(cur!=null){
            if(cur==tail&&cur.val==key){
                tail = cur.prev;
                tail.next = null;
                size--;
            }
            if(cur.val==key){
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                size--;
            }
            cur = cur.next;
        }
    }

    //得到单链表的长度
    public int size(){
        return size;
    }
    public void display(){
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public void clear(){
        if(head==null||head.next==null){
            head=null;
            tail=null;
            size=0;
            return;
        }
        ListNode cur = head.next;
        while(cur!=null){
            head.next =null;
            head.prev =null;
            head = cur;
            cur = cur.next;
        }
        head.prev=null;
        head = null;
        tail = null;
        size=0;
    }
}
